package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.ChargeTypeDao;
import com.example.domain.ChargeType;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.ChargeTypeService;
import com.example.tool.DateTool;
import com.example.treeUtils.ChargeTypeTreeUtil;
import com.example.viewInfo.ChargeTypeTreeView;

/**
 * @author 袁小弘
 * @describe 费用类型服务实现层
 * @date 2020-02-18
 */
@Service
public class ChargeTypeServiceImpl implements ChargeTypeService {

    @Autowired
    ChargeTypeDao chargeTypeDao;

    /**
     * @describe 添加费用类型信息
     */
    @Override
    public Result addChargeType(ChargeType chargeType) {
        if (chargeType != null) {
            // 获取系统当前时间作为记录创建时间
            chargeType.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        ChargeType chargeType1 = chargeTypeDao.save(chargeType);

        if (chargeType1 != null) {
            return ResultFactory.buildSuccessResult(chargeType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新费用类型信息
     */
    @Override
    public Result updateChargeType(ChargeType chargeType) {
        if (chargeType != null) {
            // 获取系统当前时间作为记录更新时间
            chargeType.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        ChargeType chargeType1 = chargeTypeDao.saveAndFlush(chargeType);

        if (chargeType1 != null) {
            return ResultFactory.buildSuccessResult(chargeType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除费用类型信息
     */
    @Override
    public Result deleteChargeTypeById(String chargeTypeId) {
        int row = chargeTypeDao.deleteByChargeTypeId(Integer.parseInt(chargeTypeId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有费用类型信息
     */
    @Override
    public Result queryAllChargeType() {
        List<ChargeType> list = chargeTypeDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据父ID查询费用类型名称信息
     */
    @Override
    public Result queryChargeTypeByPid(String pid) {
        List<ChargeType> list = chargeTypeDao.findByPid(Integer.parseInt(pid));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询费用类型信息
     */
    @Override
    public Result queryChargeTypeById(String chargeTypeId) {
        List<ChargeType> list = chargeTypeDao.findByChargeTypeId(Integer.parseInt(chargeTypeId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加费用类型信息
     */
    @Override
    public Result addChargeTypeByExcel(List<ChargeType> list) {
        List<ChargeType> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = chargeTypeDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据费用类型编号批量删除费用类型信息
     */
    @Override
    public Result deleteChargeTypeByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = chargeTypeDao.deleteByChargeTypeIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 费用类型信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<ChargeType> list = null;
//		List<ClassTypeTreeView> list2 = new ArrayList<ClassTypeTreeView>();

        if (info[0].equals("typeName")) {

            list = chargeTypeDao.findByTypeNameLike(info[1]);

        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {


            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 费用类型树形结构信息查询
     */
    @Override
    public Result queryAllOfTreeForm() {
        List<ChargeType> list = chargeTypeDao.findAll();
        List<ChargeTypeTreeView> list2 = new ArrayList<ChargeTypeTreeView>();

        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                ChargeTypeTreeView tv = new ChargeTypeTreeView();
                tv.setChargeTypeId(list.get(i).getChargeTypeId());
                tv.setPid(list.get(i).getPid());
                tv.setTypeName(list.get(i).getTypeName());
                tv.setComment(list.get(i).getComment());
                tv.setRecordTime(list.get(i).getRecordTime());
                tv.setUpdateTime(list.get(i).getUpdateTime());

                list2.add(tv);
            }
            System.out.println(list.size() + "===============" + list2.toString());
            return ResultFactory.buildSuccessResult(ChargeTypeTreeUtil.buildByRecursive(list2));
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
